var google_parser = {
  getChannel: function(){
    if (/ads/.test(window.location.href)) {
      return 'LolkingTest';
    }

    var channel = 'LolkingOther';
    var uri = window.location.pathname;
    if (uri.indexOf('/models') == 0) channel = 'LolkingModel';
    else if (uri.indexOf('/summoner') == 0) channel = 'LolkingSummoner';
    else if (uri.indexOf('/champions') == 0) channel = 'LolkingChampion';
    else if (uri.indexOf('/search') == 0) channel = 'LolkingSearch';
    else if (uri.indexOf('/charts') == 0) channel = 'LolkingChart';
    else if (uri.indexOf('/streams') == 0) channel = 'LolkingStream';
    else if (uri.indexOf('/guides') == 0) channel = 'LolkingGuides';
    else if (uri == '/') channel = 'LolkingHome';
    return channel;
  },
  getInclusion: function(config) {
    if (typeof(I11C) !== "undefined" && I11C.Morph == 1) {
      return "_Instart";
    } else {
      return "";
    }
  },
  checkScreenSize: function(slot, current_inner_width){
    if(window.innerWidth < current_inner_width || slot.min_width < current_inner_width) return false;
    return true;
  },
  allowSlot: function(slot, type, channel, current_inner_width){
    if(!slot) return false;
    if(slot.only_channels && slot.only_channels[type] && slot.only_channels[type].indexOf(channel) == -1) return false;
    if(slot.remove_channels && slot.remove_channels[type] && slot.remove_channels[type].indexOf(channel) > -1) return false;
    return true;
  },
  addATFSlot: function(config, channel){
    //Will change to be multi-sized
    if(config.adslots.atf){
      if (this.checkScreenSize(config.adslots.atf, 970)) {
        if(this.allowSlot(config.adslots.atf, "large", channel, 970)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Horizontal_ATF' + this.getInclusion(), config.adslots.atf.large, 'za-leaderboard').addService(googletag.pubads())
          googletag.pubads().addEventListener('slotRenderEnded', function(event) {
            if(event.slot == slot) {
                var adWidth = parseInt(event.size[0]);
                var adHeight = parseInt(event.size[1]);
                $('body').removeClass('has-adblocker');
                $('body').removeClass('has-970x90');
                $('body').removeClass('has-970x250');
                $('body').removeClass('has-728x90');

                if (adWidth === 970) {
                  $('body').toggleClass('has-970x90', (adHeight === 90));
                  $('body').toggleClass('has-970x250', (adHeight === 250));
                }
                if (adWidth === 728 && window.innerWidth > 1280) {
                  $('body').toggleClass('has-728x90', (adHeight === 90));
                }
                $( document ).ready(function() {
                  if (typeof readjustColumns === "function") readjustColumns();
                });
            }
          });
        }
      } else if (this.checkScreenSize(config.adslots.atf, 768)) {
        if (this.allowSlot(config.adslots.atf, "medium", channel, 768)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Horizontal_ATF' + this.getInclusion(), config.adslots.atf.medium, 'za-leaderboard').addService(googletag.pubads());
        }
      } else if (this.checkScreenSize(config.adslots.atf, 0)) {
        if(this.allowSlot(config.adslots.atf, "small", channel, 0)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Horizontal_ATF' + this.getInclusion(), config.adslots.atf.small, 'za-leaderboard').addService(googletag.pubads());
        }
      }
      return ['za-leaderboard', slot];
    }
  },
  addBTFSlot: function(config, channel){
    if(config.adslots.btf){
      if (this.checkScreenSize(config.adslots.btf, 768)) {
        if (this.allowSlot(config.adslots.btf, "medium", channel, 768)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Horizontal_BTF' + this.getInclusion(), config.adslots.btf.medium, 'bottom-leaderboard').addService(googletag.pubads());
        }
      } else if (this.checkScreenSize(config.adslots.btf, 0)) {
        if(this.allowSlot(config.adslots.btf, "small", channel, 0)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Horizontal_BTF' + this.getInclusion(), config.adslots.btf.small, 'bottom-leaderboard').addService(googletag.pubads());
        }
      }
      return ['bottom-leaderboard', slot];
    }
  },
  addATFVerticalSlot: function(config, channel){
    if(config.adslots.atf_vertical){
      if (this.checkScreenSize(config.adslots.atf_vertical, 1024)) {
        if (this.allowSlot(config.adslots.atf_vertical, "large", channel, 1024)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_ATF' + this.getInclusion(), config.adslots.atf_vertical.large, 'za-medrec-atf').addService(googletag.pubads());
          return ['za-medrec-atf', slot];
        }
      } else if (this.checkScreenSize(config.adslots.atf_vertical, 768)) {
        if (this.allowSlot(config.adslots.atf_vertical, "medium", channel, 768)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_ATF' + this.getInclusion(), config.adslots.atf_vertical.medium, 'za-medrec-atf-mob').addService(googletag.pubads());
        }
      } else if (this.checkScreenSize(config.adslots.atf_vertical, 0)) {
        if(this.allowSlot(config.adslots.atf_vertical, "small", channel, 0)){
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_ATF' + this.getInclusion(), config.adslots.atf_vertical.small, 'za-medrec-atf-mob').addService(googletag.pubads());
        }
      }
      return ['za-medrec-atf-mob', slot];
    }
  },
  addBTFVerticalSlot: function(config, channel){
    if(config.adslots.btf_vertical){
      if (this.checkScreenSize(config.adslots.btf_vertical, 1024)) {
        if (this.allowSlot(config.adslots.btf_vertical, "large", channel, 1024)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_BTF' + this.getInclusion(), config.adslots.btf_vertical.large, 'za-medrec-btf').addService(googletag.pubads());
          return ['za-medrec-btf', slot];
        }
      } else if (this.checkScreenSize(config.adslots.btf_vertical, 768)) {
        if (this.allowSlot(config.adslots.btf_vertical, "medium", channel, 768)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_BTF' + this.getInclusion(), config.adslots.btf_vertical.medium, 'za-medrec-btf-mob').addService(googletag.pubads());
        }
      } else if (this.checkScreenSize(config.adslots.btf_vertical, 0)) {
        if(this.allowSlot(config.adslots.btf_vertical, "small", channel, 0)) {
          var slot = googletag.defineSlot('/76136274/LoLKing_Vertical_BTF' + this.getInclusion(), config.adslots.btf_vertical.small, 'za-medrec-btf-mob').addService(googletag.pubads());
        }
      }
      return ['za-medrec-btf-mob', slot];
    }
  },
  setRefreshAd(slots, refresh_rate){
    var self = this;
    if(slots && slots.length > 0){
      setInterval(function(){
        googletag.pubads().refresh(slots);
      }, refresh_rate * 1000);
    }
  },
  setTO(id){
    var self = this;
    setTimeout(function(){
      if(document.getElementById(id) != null){
        googletag.cmd.push(function() { googletag.display(id); });
      }
      else {
        self.setTO(id);
      }
    }, 300);
  },
  parse: function(config) {
    if (typeof googletag !== "undefined") {
      var self = this;
      var channel = this.getChannel();
      googletag.cmd.push(function() {
        var ids = [];
        googletag.pubads().enableAsyncRendering();
        ids.push(self.addATFSlot(config, channel));
        ids.push(self.addBTFSlot(config, channel));
        ids.push(self.addATFVerticalSlot(config, channel));
        ids.push(self.addBTFVerticalSlot(config, channel));

        googletag.enableServices();
        var slots = [];
        for(var i in ids){
          if(ids[i]){
            self.setTO(ids[i][0]);
            slots.push(ids[i][1]);
          }
        }
        self.setRefreshAd(slots, config.refresh_rate);
      });
    }
  }
}
